package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.TreeMap;
import java.util.TreeSet;

import javax.swing.JFrame;

import model.kwalificatiebeheer.Competentie;
import model.kwalificatiebeheer.Criterium;
import model.kwalificatiebeheer.Werkproces;

import view.AddCompetentieToWerkproces;
import view.ViewCompetentiePanel;
import view.ViewCriteriumPanel;
import view.WerkprocesPanel;

public class WerkprocesPanelActions implements ActionListener, MouseListener{
	
	private WerkprocesPanel sender;
	
	public WerkprocesPanelActions(WerkprocesPanel sender){
		this.sender = sender;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("btnAddCompetentie")){
			new AddCompetentieToWerkproces(sender.getWerkproces());
		}else if(e.getActionCommand().equals("btnSave")){
			Werkproces wp = sender.getWerkproces();
			wp.update(sender.getTxtTitle().getText(), 
						sender.getTxtOmschrijving().getText(), 
						sender.getTxtResultaat().getText(), 
						sender.getTxtVakEnVa().getText()
					);
			JFrame frame = (JFrame)sender.getTopLevelAncestor();
			frame.dispose();
		}else if(e.getActionCommand().equals("btnDelete")){
			ArrayList<Competentie> list= new ArrayList<Competentie>(sender.getWerkproces().getCompetentieCriteria().keySet());
			Competentie com = list.get(sender.getCompetentieTable().getSelectedRow());
			sender.getWerkproces().deleteCompetentie(com);
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		int row = sender.getCompetentieTable().getSelectedRow();
		ArrayList<Competentie> cList = new ArrayList<Competentie>(sender.getWerkproces().getCompetentieCriteria().keySet());
		Competentie c = cList.get(row);
		if(sender.getActiveCompetentie() != null)
			sender.getActiveCompetentie().deleteObserver(sender);
		
		sender.setActiveCompetentie(c);
		c.addObserver(sender);
		
		Criterium cr = sender.getWerkproces().getCompetentieCriteria().get(c);
		if(sender.getActiveCriterium() != null)
			sender.getActiveCriterium().deleteObserver(sender);
		
		sender.setActiveCriterium(cr);
		cr.addObserver(sender);
		
		if(e.getClickCount() == 2){
			int col = sender.getCompetentieTable().getSelectedColumn();
			JFrame f = new JFrame();
			f.setVisible(true);
			f.setSize(500, 300);
			if(col == 0){
				f.setContentPane(new ViewCompetentiePanel(sender.getActiveCompetentie()));
			}else if(col == 1){
				f.setContentPane(new ViewCriteriumPanel(sender.getActiveCriterium()));
			}
		}
	}

	@Override
	public void mouseEntered(MouseEvent e) {}
	@Override
	public void mouseExited(MouseEvent e) {}
	@Override
	public void mousePressed(MouseEvent e) {}
	@Override
	public void mouseReleased(MouseEvent e) {}

}
